## Create Figure A5

## Loading packages
library(readxl)
library(tidyverse)
library(lubridate)
library(sf)
library(foreign)
library(haven)

## Load data
dta = read_stata("dta.dta")

## Share of spending in each category
tech_spend = sum(dta$ldp_tech, na.rm = T)/sum(dta$ldp_amount)*100 ## 0.9062384
tech_spend
security_spend = sum(dta$ldp_security, na.rm = T)/sum(dta$ldp_amount)*100 ## 38.38348
security_spend
interior_spend = sum(dta$ldp_interior, na.rm = T)/sum(dta$ldp_amount)*100 ## 20.99917
interior_spend
exterior_spend = sum(dta$ldp_exterior, na.rm = T)/sum(dta$ldp_amount)*100 ##  39.56049
exterior_spend
other_spend = sum(dta$ldp_other, na.rm = T)/sum(dta$ldp_amount)*100 ## 0.1506192
other_spend

## Share of projects in each category
tech_proj = sum(dta$ldp_tech_n, na.rm = T)/sum(dta$ldp_n, na.rm = T)*100 ##  1.707317
tech_proj
security_proj = sum(dta$ldp_security_n, na.rm = T)/sum(dta$ldp_n, na.rm = T)*100 ## 49.02439
security_proj
interior_proj = sum(dta$ldp_interior_n, na.rm = T)/sum(dta$ldp_n, na.rm = T)*100 ## 16.34146
interior_proj
exterior_proj = sum(dta$ldp_exterior_n, na.rm = T)/sum(dta$ldp_n, na.rm = T)*100 ## 32.68293
exterior_proj
other_proj = sum(dta$ldp_other_n, na.rm = T)/sum(dta$ldp_n, na.rm = T)*100 ## 0.2439024
other_proj

## Plot
shares = c(tech_spend, security_spend, interior_spend,
           exterior_spend, other_spend, tech_proj, security_proj,
           interior_proj, exterior_proj, other_proj)
cat = c("Technology", "Security", "Interior", "Exterior", "Other",
        "Technology", "Security", "Interior", "Exterior", "Other")
type = c("Funding", "Funding", "Funding", "Funding", "Funding",
         "Projects", "Projects", "Projects", "Projects", "Projects")
ldp_shares = data.frame(shares, cat, type)

ldp_cat_shares = ggplot(ldp_shares, aes(x = factor(cat, level = c("Technology", "Security", "Exterior", "Interior", "Other")), y = shares,
                                        fill = factor(type))) + 
  geom_bar(stat = "identity", position = "dodge") +
  geom_text(aes(label=
                  paste0(I(round(shares, digits = 2)), "%")), 
            position=position_dodge(width=0.9), vjust=-0.45, size = 2) +
  scale_fill_manual(name = "Share of . . .", values = c("gray75", "gray45"),
                    labels = c("Funding", "Projects")) +
  labs(x = "Project Type", y = "Share") +
  scale_y_continuous(limits = c(0, 55), breaks = c(0, 10, 20, 30, 40, 50),
                     labels = function(x) paste0(x, "%")) +
  theme_bw() +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_rect(colour = "black"),
        legend.position = "bottom")


